

<title>选课功能-用户申请</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>选课</cite></a>
    <a><cite>用户申请</cite></a>
    <a><cite>内测</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">内测用户列表</div>
        <div class="layui-card-body">
          <table class="layui-hide" id="test-table-page" lay-filter="test"></table>
        </div>
      </div>
    </div>
  </div>
</div>

<script type="text/html" id="lineBar">
  <a class="layui-btn layui-btn-xs" lay-event="info">资料</a>
  <a class="layui-btn layui-btn-xs" lay-event="agree">同意</a>
  <a class="layui-btn layui-btn-xs" lay-event="refuse">拒绝</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarlist">
  <div class="layui-form-item">
    <div class="layui-input-inline">
      <select lay-filter="selectFilter" id="selectFilter" name="filtertype" lay-verify="required">
        <option value="all">全部</option>
        <option value="refuse">已拒绝</option>
        <option value="agree">已同意</option>
        <option value="wait">待决定</option>
      </select>
    </div>
    <div class="layui-input-inline">
      <select lay-filter="batchOp" id="batchOp" name="batchOp">
        <option value>批量操作</option>
        <option value="batchAgree">批量同意</option>
        <option value="batchRefuse">批量拒绝</option>
        <option value="batchDel">批量删除</option>
      </select>
    </div>
  </div>
</script>

<script>
  layui.use(['jquery', 'table'], function(){
    var $ = layui.$, form = layui.form, table = layui.table;
    var filterType = "all";
    var delUrl = "/uclass/admin/elective/apply/del";
    var agreeUrl = "/uclass/admin/elective/apply/agree";
    var refuseUrl = "/uclass/admin/elective/apply/refuse";
    var userlistUrl = "/uclass/admin/elective/apply";

    // 专业集合
    var specialtySet = {};

    function getStatus(status) {
      switch (status) {
        case 0:
          return "<span style=\"color: #c23f34;\">已拒绝</span>";
        case 1:
          return "<span style=\"color: #5FB878;\">已同意</span>"
        case 2:
          return "<span style=\"color: #4a95ff\">待审核</span>"
      }
    }
    function timestampToTime(timestamp) {
      var date = new Date(timestamp);
      Y = date.getFullYear() + '-';
      M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      D = change(date.getDate()) + ' ';
      h = change(date.getHours()) + ':';
      m = change(date.getMinutes()) + ':';
      s = change(date.getSeconds());
      return Y + M + D + h + m + s;
    }
    function change(t) {
      if (t < 10) {
        return "0" + t;
      } else {
        return t;
      }
    }

    var tablelns = table.render({
      elem: '#test-table-page'
      ,url: userlistUrl
      ,toolbar: '#toolbarlist'
      ,where: {"type": filterType}
      ,cols: [[
        {type:'checkbox', fixed:'left'}
        ,{field:'usercode', minWidth:120, title: '学号', sort: true, align: 'center'}
        ,{field:'time', minWidth:150, title:'申请时间', align:'center', templet: function (d) { return timestampToTime(d.time) }}
        ,{field: 'phone', minWidth:150, title:'手机'}
        ,{field: 'email', minWidth:210, title: '邮箱'}
        ,{field:'status', minWidth:80, title: '状态', align: 'center', templet: function(d) {return getStatus(d.status) }}
        ,{align:'center', minWidth:220, title:'操作', toolbar:'#lineBar'}
      ]]
      ,page: true
      ,done: function (res, curr, count) {
          $('#selectFilter').val(filterType);
          form.render('select');
      }
      ,parseData: function(res) {
        return {
          "status": res.status,
          "message": res.message,
          "total": res.data.total,
          "list": res.data.list
        };
      }
      ,response: {
        statusName: 'status'
        ,statusCode: 200
        ,msgName: 'message'
        ,countName: 'total'
        ,dataName: 'list'
      }
    });
    function post(url, data) {
      var status = 0;
      $.ajax({
        async:      false,
        url:        url,
        dataType:   'json',
        data:       {list:data},
        type:       'POST',
        complete    :function (result) {
          status = result.responseJSON.status;
        }
      });
      return status;
    }
    function clickDel(obj) {
      layer.confirm('确定删除?', function (index) {
        var load = layer.load(2);
        var url = delUrl;
        var usercode = obj.data.usercode;
        var result = post(url, [usercode]);
        layer.close(load);
        if (result == 200) {
          layer.msg("删除成功");
          obj.del();
        }else {
          layer.msg("删除失败");
        }
        layer.close(index);
      });
    }
    function clickAgree(obj) {
      layer.confirm('确定同意?', function (index) {
        var load = layer.load(2);
        var url = agreeUrl;
        var usercode = obj.data.usercode;
        var result = post(url, [usercode]);
        layer.close(load);
        if (result == 200) {
          layer.msg("操作成功");
          obj.update({
            status: 1
          });
        }else {
          layer.msg("操作失败");
        }
        layer.close(index);
      });
    }
    function clickRefuse(obj) {
      layer.confirm('确定拒绝?', function (index) {
        var load = layer.load(2);
        var url = refuseUrl;
        var usercode = obj.data.usercode;
        var result = post(url, [usercode]);
        layer.close(load);
        if (result == 200) {
          layer.msg("操作成功");
          obj.update({
            status: 0
          });
        }else {
          layer.msg("操作失败");
        }
        layer.close(index);
      });
    }

    function clickInfo(obj) {
      var usercode = obj.data.usercode;
      $.ajax({
        url: '/uclass/admin/user/info?usercode=' + usercode,
        type: 'GET',
        dataType: 'JSON',
        complete: function (result) {
          result = result.responseJSON;
          var data = result.data;
          var name = data.name;
          var year = data.grade;
          var specialtyCode = data.code;
          if (specialtySet[year] === undefined) {
            $.ajax({
              url: "/uclass/elective/specialty?year=" + year,
              async: false,
              dataType: 'json',
              complete: function (result) {
                result = result.responseJSON;
                specialtySet[year] = result.data;
              }
            })
          }
          var list = specialtySet[year];
          for(var specialtyIndex in list) {
            var specialty = list[specialtyIndex];
            if (specialty.code == specialtyCode) {
              layer.msg(name + " " + year + " " + specialty.name)
              break;
            }
          }
        }
      })
    }
    table.on('tool(test)', function (obj) {
        switch (obj.event) {
          case 'info':
            clickInfo(obj);
            break;
          case 'del':
            clickDel(obj);
            break;
          case 'agree':
            clickAgree(obj);
            break;
          case 'refuse':
            clickRefuse(obj);
            break;
        }
    });

    form.on('select(selectFilter)', function (data) {
      filterType = data.value;
      tablelns.reload({
        where: {type: data.value}
        ,page: {curr:1}
      });
    });

    form.on('select(batchOp)', function (data) {
        var op = data.value;
        var checkStatus = table.checkStatus("test-table-page");
        var data = checkStatus.data;
        var userList = [];
        var length = data.length;
        if (length == 0){
          layer.msg("请选择至少一项");
          return;
        }
        for (var i = 0; i < length; i++) {
          userList.push(data[i].usercode)
        }
        var result = 0;
        switch (op) {
          case 'batchAgree':
            layer.confirm("确认同意？", function (index) {
              result = post(agreeUrl, userList);
              if (result == 200) {
                layer.msg("操作成功");
              } else {
                layer.msg("操作失败");
              };
              tablelns.reload();
              layer.close(index);
            });
            break;
          case 'batchRefuse':
            layer.confirm("确认拒绝?", function (index) {
              result = post(refuseUrl, userList);
              if (result == 200) {
                layer.msg("操作成功");
              } else {
                layer.msg("操作失败");
              }
              tablelns.reload();
              layer.close(index);
            });

            break;
          case 'batchDel':
            layer.confirm("确认删除？", function (index) {
              result = post(delUrl, userList);
              if (result == 200) {
                layer.msg("操作成功");
              } else {
                layer.msg("操作失败");
              };
              tablelns.reload();
              layer.close(index);
            });
            break;
        }
    })
  });
</script>